//
//  CYLHomeViewController.m
//  CYLTabBarController


#import "CYLHomeViewController.h"



#import <MJRefresh/MJRefresh.h>
#import "App_devices_model.h"

#import "SDCycleScrollView.h"
#import "HomeDevice_tvCell.h"

#import "DeviceVc.h"
#import "QQLBXScanViewController.h"

#import "Global.h"

#import "CYLPlusDemoChildViewController.h"

#import "Cwgvc.h"

@interface CYLHomeViewController()<SDCycleScrollViewDelegate, UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIImageView *at_img_iv;
@property (weak, nonatomic) IBOutlet UIView *jiaju_tip_v;
@property (weak, nonatomic) IBOutlet UILabel *jiaju_tip_lb;
@property (weak, nonatomic) IBOutlet UIButton *add_jiaju_btn;
@property (weak, nonatomic) IBOutlet SDCycleScrollView *cycleScrollView;
@property (weak, nonatomic) IBOutlet UILabel *zanwu_lb;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArr;

@property (nonatomic, assign) NSTimeInterval custom_acceptEventTime;
@property (nonatomic, assign) NSTimeInterval custom_acceptEventInterval;

@end

@implementation CYLHomeViewController

- (IBAction)add_action:(id)sender {
    
    [self add_jiaju_action:sender];
}

- (IBAction)bar_rightItem_action:(UIButton *)sender {

    //添加一些扫码或相册结果处理
    QQLBXScanViewController *vc = [QQLBXScanViewController new];
    vc.libraryType = [Global sharedManager].libraryType;
    vc.scanCodeType = [Global sharedManager].scanCodeType;
    
    
    //设置扫码区域参数设置
    
    //创建参数对象
    LBXScanViewStyle *style = [[LBXScanViewStyle alloc]init];
    
    //矩形区域中心上移，默认中心点为屏幕中心点
    style.centerUpOffset = 44;
    
    //扫码框周围4个角的类型,设置为外挂式
    style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle_Outer;
    
    //扫码框周围4个角绘制的线条宽度
    style.photoframeLineW = 6;
    
    //扫码框周围4个角的宽度
    style.photoframeAngleW = 24;
    
    //扫码框周围4个角的高度
    style.photoframeAngleH = 24;
    
    //扫码框内 动画类型 --线条上下移动
    style.anmiationStyle = LBXScanViewAnimationStyle_LineMove;
    
    //线条上下移动图片
    style.animationImage = [UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_light_green"];
    
    style.notRecoginitonArea = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    
    vc.style = style;
    
    //镜头拉远拉近功能
    vc.isVideoZoom = YES;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)add_jiaju_action:(UIButton *)sender {
    CYLPlusDemoChildViewController *vc= [[CYLPlusDemoChildViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - View lifecycle
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self refresh];
}
- (void)initSubviews{
    [super initSubviews];
    self.dataArr = [NSMutableArray array];
    self.jiaju_tip_v.hidden = YES;
    self.jiaju_tip_lb.hidden = YES;
    self.at_img_iv.hidden = YES;
    self.tableView.hidden = YES;
    self.add_jiaju_btn.hidden = YES;
    self.zanwu_lb.hidden = YES;
    
    self.tableView.tableFooterView = [UIView new];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeDevice_tvCell" bundle:nil] forCellReuseIdentifier:@"HomeDevice_tvCell"];
    
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    
    //    NSArray *imagesURLStrings = @[@"https://ss2.jpg",@"https://ss2.jpg",@"https://ss2.jpg",@"https://ss2.jpg"];
    // 图片配文字
    //    NSArray *titles = @[@"感谢",@"感谢",@"感谢"];
    //    self.cycleScrollView.titlesGroup = titles;
    //    self.cycleScrollView.imageURLStringsGroup = imagesURLStrings;
    //    self.cycleScrollView.backgroundColor = UIColor.whiteColor;
    
    self.cycleScrollView.layer.shadowColor = UIColor.lightGrayColor.CGColor;
    self.cycleScrollView.layer.shadowRadius = 10;
    self.cycleScrollView.layer.shadowOffset = CGSizeMake(10, 10);
    self.cycleScrollView.layer.shadowOpacity = 0.4;
    self.cycleScrollView.layer.cornerRadius = 10;
    self.cycleScrollView.clipsToBounds = NO;
    
    self.cycleScrollView.localizationImageNamesGroup = @[@"banner1", @"banner2", @"banner3", @"banner4", ];
    self.cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    self.cycleScrollView.delegate = self;
    self.cycleScrollView.currentPageDotColor = themeColor; // 自定义分页控件小圆标颜色
    //    self.cycleScrollView.placeholderImage = [UIImage imageNamed:@"banner1"];
    
    
    
    self.at_img_iv.layer.shadowColor = UIColor.lightGrayColor.CGColor;
    self.at_img_iv.layer.shadowRadius = 10;
    self.at_img_iv.layer.shadowOffset = CGSizeMake(10, 10);
    self.at_img_iv.layer.shadowOpacity = 0.4;
    self.at_img_iv.layer.cornerRadius = 10;
    self.at_img_iv.clipsToBounds = NO;
    
    self.add_jiaju_btn.layer.borderColor = themeColor.CGColor;
    self.add_jiaju_btn.layer.borderWidth = 0.6;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
  
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}

#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    DLog();
}

- (void)refresh {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:app_code forKey:@"app_code"];
    [dict setObject:AuthSignin_model.sharedInstance.data.token forKey:@"Authorization"];
    
    [PPHTTPRequest req:@"api/App/devices" parameters:dict success:^(id response) {
        NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
        if ([error_code isEqualToString:@"0"]) {
            App_devices_model *data = [App_devices_model mj_objectWithKeyValues:response];
            if (data.data.count > 0) {
                self.dataArr = data.data.mutableCopy;
                [self.tableView reloadData];
                self.jiaju_tip_v.hidden = NO;
                self.jiaju_tip_lb.hidden = NO;
                self.at_img_iv.hidden = YES;
                self.tableView.hidden = NO;
                self.add_jiaju_btn.hidden = YES;
                self.zanwu_lb.hidden = YES;
            }else{
                self.tableView.hidden = YES;
                self.jiaju_tip_lb.hidden = YES;
                self.jiaju_tip_v.hidden = YES;
                self.at_img_iv.hidden = NO;
                self.add_jiaju_btn.hidden = NO;
                self.zanwu_lb.hidden = NO;
            }
        }
        
        else
            //            if ([error_code isEqualToString:@""])
        {
            
            [MBProgressHUD showTipMessageInView:response[@"error_msg"]];
            
        }
    } failure:^(NSError *error) {
        [MBProgressHUD showTipMessageInView:[error localizedDescription]];
        
    }];
}





#pragma mark - Table view
#pragma mark - ================== table view data source delegate ==================
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeDevice_tvCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeDevice_tvCell"];
    if (!cell) {
        cell = [[HomeDevice_tvCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HomeDevice_tvCell"];
    }
    App_devicesData *list = self.dataArr[indexPath.row];
    cell.entity = list;
    
    cell.blk = ^(NSString * _Nonnull name_new) {
      
        [self refresh];
    };
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    App_devicesData *list = self.dataArr[indexPath.row];
    

    if ([list.ui_code isEqualToString:@"mao"] || [list.device_type isEqualToString:@"YA-SPC-20A"]) {
        // 宠物柜
        Cwgvc *vc= [[Cwgvc alloc]init];
        vc.entity = list;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }else if([list.ui_code isEqualToString:@"yiner-app"] || [list.device_type isEqualToString:@"YA-HBB-19A"] || [list.device_type isEqualToString:@"C4-4F-33-14-EC-09"]){
        // 婴儿床
        DeviceVc *vc= [[DeviceVc alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.entity = list;
        vc.titleColorSelected = themeColor;
        vc.titleColorNormal = rgb(153,153,153);
        vc.titleSizeNormal = 17;
        vc.titleSizeSelected = 20;
        [vc setMenuViewStyle:WMMenuViewStyleLine];
        vc.progressWidth = 40;
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    /*
    
   // 值得提醒一下：如果这里设置了统一的时间间隔，会影响UISwitch,如果想统一设置，又不想影响UISwitch，建议将UIControl分类，改成UIButton分类，实现方法是一样的
    if (self.custom_acceptEventInterval <= 0) {
        // 如果没有自定义时间间隔，则默认为2秒
        self.custom_acceptEventInterval = 2;
    }
    // 是否小于设定的时间间隔
    BOOL needSendAction = (NSDate.date.timeIntervalSince1970 - self.custom_acceptEventTime >= self.custom_acceptEventInterval);
    // 更新上一次点击时间戳
    if (self.custom_acceptEventInterval > 0) {
        self.custom_acceptEventTime = NSDate.date.timeIntervalSince1970;
    }
    
    
    // 两次点击的时间间隔小于设定的时间间隔时，才执行响应事件
    if (needSendAction) {
        App_devicesData *list = self.dataArr[indexPath.row];

           // 从服务器获取音乐列表
           NSMutableDictionary *dict = [NSMutableDictionary dictionary];
           [dict setObject:list.product_id forKey:@"product_id"];
           [dict setObject:AuthSignin_model.sharedInstance.data.token forKey:@"Authorization"];
       
        __block QMUITips *qmui_tip = [QMUITips showLoading:@"连接中..." inView:self.navigationController.view];
           
           [PPHTTPRequest req:@"api/App/getMusicList" parameters:dict success:^(id response) {
               NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
               if ([error_code isEqualToString:@"0"]) {
                   App_getMusicList *musicList = [App_getMusicList mj_objectWithKeyValues:response];
                   
                   for (NSInteger i = 0; i < musicList.data.music_list.count; i++) {
                       App_getMusicListMusic_list *song = musicList.data.music_list[i];
                       song.index = i;
                   }
                   [qmui_tip hideAnimated:YES];

                   DeviceVc *vc= [[DeviceVc alloc]init];
                   vc.hidesBottomBarWhenPushed = YES;
                   vc.entity = list;
                   vc.musicList = musicList;
                   vc.titleColorSelected = themeColor;
                   vc.titleColorNormal = rgb(153,153,153);
                   vc.titleSizeNormal = 17;
                   vc.titleSizeSelected = 20;
                   [vc setMenuViewStyle:WMMenuViewStyleLine];
                   vc.progressWidth = 40;
                   [self.navigationController pushViewController:vc animated:YES];
               }
               
               else
                   //            if ([error_code isEqualToString:@""])
               {
                   
                   [qmui_tip showError:response[@"error_msg"] hideAfterDelay:2];

                   
               }
           } failure:^(NSError *error) {
               [qmui_tip showError:[error localizedDescription] hideAfterDelay:2];

               
           }];

    }
    
   
   */
}





@end
